<template>
  <van-row class="ordering-dishes">
    <van-nav-bar left-arrow @click-left="onClickLeft" @click="orderingDishes">
      <template #title>
        {{ $moment(state.selectDate).format("YYYY-MM-DD") }}
        <van-icon color="#000" name="arrow-down" />
      </template>
    </van-nav-bar>
    <van-calendar
      v-model:show="state.show"
      @confirm="confirm"
      :max-date="state.maxDate"
    />
    <!--选择日期后的数据 -->
    <div
      v-for="item in state.orderingDishesList"
      style="width: 100%"
      :key="item.id"
      @click="orderEvent(item)"
    >
      <div style="padding: 10px 10px">
        <van-card
          style="border-radius: 10px"
          :class="{ noActive: !item.active }"
        >
          <template #title>
            <div
              v-if="item.active"
              style="
                font-size: 22px;
                font-weight: bold;
                text-align: left;
                color: #08d781;
              "
            >
              {{ item.time }}
            </div>
            <div
              v-else
              style="font-size: 22px; font-weight: bold; text-align: left"
            >
              {{ item.time }}
            </div>
          </template>
          <template #desc>
            <van-row class="time-content">
              <van-col span="17">
                <div style="text-align: left; font-size: 16px">
                  {{ item.text }}
                </div>
              </van-col>
              <van-col span="7">
                <van-button
                  style="
                    background-color: #a8a8a8;
                    border-radius: 10px;
                    border: none;
                    width: 80px;
                    color: #fff;
                    margin-left: 20px;
                  "
                  size="mini"
                  v-if="!item.active"
                  >已关闭</van-button
                >
                <van-button
                  style="
                    background-color: #08d781;
                    border-radius: 10px;
                    border: none;
                    width: 80px;
                    color: #fff;
                    margin-left: 20px;
                  "
                  size="mini"
                  v-else
                  >可点餐</van-button
                >
              </van-col>
            </van-row>
          </template>
        </van-card>
      </div>
    </div>
  </van-row>
</template>
<script setup>
import { reactive, onBeforeMount } from "vue";
import { useRoute, useRouter } from "vue-router";
import { findByName } from "@/api/orderingDishes/orderingDishes";
import moment from "moment";
const router = useRouter();
const route = useRoute();
const state = reactive({
  wechatCode: route.query.code,
  navActive: 1,
  selectDate: new Date(), //选择的日期
  show: false,
  mealListData: "",
  orderingDishesList: [
    {
      id: 1,
      text: "中南大学湘雅医院外卖前一天20:00前预定客服电话19386674470【早餐】08:00送达",
      time: "8:00",
      active: false,
    },
    {
      id: 2,
      text: "中南大学湘雅医院外卖10:00前预定客服电话19386674470【午餐】10:30送达",
      time: "10:30",
      active: false,
    },
    {
      id: 3,
      text: "中南大学湘雅医院外卖16:00前预定客服电话19386674470【晚餐】17:30送达",
      time: "17:30",
      active: false,
    },
    // { id: 4, text: "中南大学湘雅医院(医院外卖)全天", time: "23:59", type: 0 },
  ],
});
// 获取
const queryOrderingDishesData = () => {
  findByName({ name: "OrderingConfiguration" }).then((v) => {
    if (v.data.value) {
      state.mealListData = JSON.parse(v.data.value);
    }
    console.log(state.mealListData);
    timeLimitation();
    determineTime();
  });
};
queryOrderingDishesData();

// 对比日期大小
function compareDates(date1, date2) {
  const year1 = new Date(date1).getFullYear();
  const year2 = new Date(date2).getFullYear();
  const month1 = new Date(date1).getMonth() + 1; // 月份从0开始，需要加1
  const month2 = new Date(date2).getMonth() + 1;
  const day1 = new Date(date1).getDate();
  const day2 = new Date(date2).getDate();

  if (year1 > year2) {
    return 1;
  } else if (year1 === year2) {
    if (month1 > month2) {
      return 1;
    } else if (month1 === month2) {
      if (day1 > day2) {
        return 1;
      } else if (day1 === day2) {
        return 0;
      } else {
        return -1;
      }
    } else {
      return -1;
    }
  } else {
    return -1;
  }
}
// 判断时间
const determineTime = () => {
  const today = new Date(); // 当前日期
  const tomorrow = new Date(state.selectDate); // 复制当前日期
  tomorrow.setDate(tomorrow.getDate() - 1); //当前日期减一天
  const selectDateDay = new Date(state.selectDate); //选择的日期
  const currentTime = new Date().getTime(); //当前日期时间戳
  const weekDay = state.selectDate.getDay(); //当前周几
  const defaultTime = ["00", "00"];
  // debugger;
  if (state.mealListData) {
    if (weekDay >= 1 && weekDay <= 5) {
      const breakfast = state.mealListData.mealList[0]; //早餐规则
      const lunch = state.mealListData.mealList[1]; //午餐规则
      const dinner = state.mealListData.mealList[2]; //晚餐规则

      if (breakfast.open) {
        const breakfastTime = breakfast.timeData
          ? breakfast.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(breakfastTime[0])
          .minutes(breakfastTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 早餐判断
        state.orderingDishesList[0].active =
          breakfast.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(breakfastTime[0])
                .minutes(breakfastTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[0].active = false;
      }
      if (lunch.open) {
        const lunchTime = lunch.timeData
          ? lunch.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(lunchTime[0])
          .minutes(lunchTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 午餐判断
        state.orderingDishesList[1].active =
          lunch.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(lunchTime[0])
                .minutes(lunchTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[1].active = false;
      }
      if (dinner.open) {
        const dinnerTime = dinner.timeData
          ? dinner.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(dinnerTime[0])
          .minutes(dinnerTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 晚餐判断
        state.orderingDishesList[2].active =
          dinner.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(dinnerTime[0])
                .minutes(dinnerTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[2].active = false;
      }
    } else {
      const breakfast = state.mealListData.weekendMealList[0]; //早餐规则
      const lunch = state.mealListData.weekendMealList[1]; //午餐规则
      const dinner = state.mealListData.weekendMealList[2]; //晚餐规则
      if (breakfast.open) {
        const breakfastTime = breakfast.timeData
          ? breakfast.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(breakfastTime[0])
          .minutes(breakfastTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 早餐判断
        state.orderingDishesList[0].active =
          breakfast.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(breakfastTime[0])
                .minutes(breakfastTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[0].active = false;
      }
      if (lunch.open) {
        const lunchTime = lunch.timeData
          ? lunch.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(lunchTime[0])
          .minutes(lunchTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 午餐判断
        state.orderingDishesList[1].active =
          lunch.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(lunchTime[0])
                .minutes(lunchTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[1].active = false;
      }
      if (dinner.open) {
        const dinnerTime = dinner.timeData
          ? dinner.timeData.split(":")
          : defaultTime;
        var yesterday = moment(state.selectDate)
          .hour(dinnerTime[0])
          .minutes(dinnerTime[1])
          .clone()
          .subtract(1, "days")
          .valueOf();

        // 晚餐判断
        state.orderingDishesList[2].active =
          dinner.timeDay == 1
            ? currentTime < yesterday
            : currentTime <
              moment(state.selectDate)
                .hour(dinnerTime[0])
                .minutes(dinnerTime[1])
                .valueOf();
      } else {
        state.orderingDishesList[2].active = false;
      }
    }
    // 特殊
    state.mealListData.special.list.forEach((value) => {
      if (value.week == weekDay) {
        if (value.open) {
          const dateTime = value.timeData
            ? value.timeData.split(":")
            : defaultTime;

          var yesterday = moment(state.selectDate)
            .hour(dateTime[0])
            .minutes(dateTime[1])
            .clone()
            .subtract(1, "days")
            .valueOf();
          console.log(
            currentTime <
              moment(state.selectDate)
                .hour(dateTime[0])
                .minutes(dateTime[1])
                .valueOf()
          );
          state.orderingDishesList[value.mealType].active =
            value.timeDay == 1
              ? currentTime < yesterday
              : currentTime <
                moment(state.selectDate)
                  .hour(dateTime[0])
                  .minutes(dateTime[1])
                  .valueOf();
        } else {
          state.orderingDishesList[value.mealType].active = false;
        }
      }
    });
  } else {
    console.log(7897899);
    state.orderingDishesList[0].active = false;
    state.orderingDishesList[1].active = false;
    state.orderingDishesList[2].active = false;
  }

  // if (weekDay >= 1 && weekDay <= 5) {
  //   if (compareDates(selectDateDay, today) != 1) {
  //     const breakfast = state.mealListData.mealList[0];
  //     const lunch = state.mealListData.mealList[1];
  //     const dinner = state.mealListData.mealList[2];
  //     const breakfastTime = breakfast.timeData
  //       ? breakfast.timeData.split(":")
  //       : defaultTime;
  //     const lunchTime = lunch.timeData
  //       ? lunch.timeData.split(":")
  //       : defaultTime;
  //     const dinnerTime = dinner.timeData
  //       ? dinner.timeData.split(":")
  //       : defaultTime;
  //     // 早餐
  //     const breakfastTimeIsValid =
  //       breakfast.timeDay == 1
  //         ? currentTime <
  //           moment()
  //             .hour(breakfastTime[0])
  //             .date(tomorrow.getDate())
  //             .minutes(breakfastTime[1])
  //             .valueOf()
  //         : currentTime <
  //           moment().hour(breakfastTime[0]).minutes(breakfastTime[1]);
  //     // 判断早餐是否可以点
  //     state.orderingDishesList[0].active =
  //       breakfast.open && breakfastTimeIsValid ? true : false;
  //     /*------------------------------------------*/
  //     // 午餐
  //     const lunchTimeIsValid =
  //       lunch.timeDay == 1
  //         ? currentTime <
  //           moment()
  //             .hour(lunchTime[0])
  //             .date(tomorrow.getDate())
  //             .minutes(lunchTime[1])
  //             .valueOf()
  //         : currentTime < moment().hour(lunchTime[0]).minutes(lunchTime[1]);
  //     // 判断午餐是否可以点
  //     state.orderingDishesList[1].active =
  //       lunch.open && lunchTimeIsValid ? true : false;
  //     console.log(lunchTimeIsValid);
  //     /*------------------------------------------*/
  //     // 晚餐
  //     const dinnerTimeIsValid =
  //       dinner.timeDay == 1
  //         ? currentTime <
  //           moment()
  //             .hour(dinnerTime[0])
  //             .date(tomorrow.getDate())
  //             .minutes(dinnerTime[1])
  //             .valueOf()
  //         : currentTime < moment().hour(dinnerTime[0]).minutes(dinnerTime[1]);
  //     // 判断晚餐是否可以点
  //     state.orderingDishesList[2].active =
  //       dinner.open && dinnerTimeIsValid ? true : false;
  //   } else {
  //     const breakfast = state.mealListData.mealList[0];
  //     const dinner = state.mealListData.mealList[2];
  //     const breakfastTime = breakfast.timeData
  //       ? breakfast.timeData.split(":")
  //       : defaultTime;
  //     const lunch = state.mealListData.mealList[1];
  //     // 早餐
  //     const breakfastTimeIsValid =
  //       breakfast.timeDay == 1
  //         ? currentTime <
  //           moment()
  //             .hour(breakfastTime[0])
  //             .date(tomorrow.getDate())
  //             .minutes(breakfastTime[1])
  //             .valueOf()
  //         : currentTime <
  //           moment()
  //             .date(selectDateDay.getDate())
  //             .hour(breakfastTime[0])
  //             .minutes(breakfastTime[1])
  //             .valueOf();

  //     // 判断早餐是否可以点
  //     state.orderingDishesList[0].active =
  //       breakfast.open && breakfastTimeIsValid ? true : false;
  //     /*------------------------------------------*/
  //     state.orderingDishesList[1].active = lunch.open ? true : false;
  //     state.orderingDishesList[2].active = dinner.open ? true : false;
  //   }
  // } else if (weekDay == 6 || weekDay == 7) {
  //   const breakfast = state.mealListData.weekendMealList[0];
  //   const breakfastTime = breakfast.timeData
  //     ? breakfast.timeData.split(":")
  //     : defaultTime;
  //   const lunch = state.mealListData.weekendMealList[1];
  //   const lunchTime = lunch.timeData ? lunch.timeData.split(":") : defaultTime;
  //   const dinner = state.mealListData.weekendMealList[2];
  //   const dinnerTime = dinner.timeData
  //     ? dinner.timeData.split(":")
  //     : defaultTime;
  //   // 早餐
  //   const breakfastTimeIsValid =
  //     breakfast.timeDay == 1
  //       ? currentTime <
  //         moment()
  //           .date(tomorrow.getDate())
  //           .hour(breakfastTime[0])
  //           .minutes(breakfastTime[1])
  //           .valueOf()
  //       : currentTime <
  //         moment().hour(breakfastTime[0]).minutes(breakfastTime[1]);
  //   // 判断早餐是否可以点
  //   state.orderingDishesList[0].active =
  //     breakfast.open && breakfastTimeIsValid ? true : false;
  //   /*------------------------------------------*/

  //   // 午餐
  //   const lunchTimeIsValid =
  //     lunch.timeDay == 1
  //       ? currentTime <
  //         moment()
  //           .hour(lunchTime[0])
  //           .date(tomorrow.getDate())
  //           .minutes(lunchTime[1])
  //           .valueOf()
  //       : currentTime <
  //         moment()
  //           .date(selectDateDay.getDate())
  //           .hour(lunchTime[0])
  //           .minutes(lunchTime[1])
  //           .valueOf();
  //   // 判断午餐是否可以点
  //   state.orderingDishesList[1].active =
  //     lunch.open && lunchTimeIsValid ? true : false;
  //   /*------------------------------------------*/
  //   // 晚餐
  //   const dinnerTimeIsValid =
  //     dinner.timeDay == 1
  //       ? currentTime <
  //         moment()
  //           .hour(dinnerTime[0])
  //           .date(tomorrow.getDate())
  //           .minutes(dinnerTime[1])
  //           .valueOf()
  //       : currentTime <
  //         moment()
  //           .date(selectDateDay.getDate())
  //           .hour(dinnerTime[0])
  //           .minutes(dinnerTime[1])
  //           .valueOf();
  //   // 判断晚餐是否可以点
  //   state.orderingDishesList[2].active =
  //     dinner.open && dinnerTimeIsValid ? true : false;
  // }

  // 特殊配置
  // state.mealListData.special.list.forEach((value) => {
  //   if (value.open) {
  //     if (value.week == weekDay) {
  //       const breakfastTime = value.timeData
  //         ? value.timeData.split(":")
  //         : defaultTime;
  //       const breakfastTimeIsValid =
  //         value.timeDay == 1
  //           ? currentTime <
  //             moment()
  //               .hour(breakfastTime[0])
  //               .date(tomorrow.getDate())
  //               .minutes(breakfastTime[1])
  //               .valueOf()
  //           : currentTime <
  //             moment().hour(breakfastTime[0]).minutes(breakfastTime[1]);

  //       state.orderingDishesList[value.mealType].active = breakfastTimeIsValid
  //         ? true
  //         : false;
  //     }
  //   }
  // });
};

// 返回上一页
const onClickLeft = () => {
  router.go(-1);
};
// 点击点餐
const orderingDishes = () => {
  state.show = true;
};
// 点击确认拿到数据
const confirm = (values) => {
  state.selectDate = values;
  state.show = false;
  determineTime();
};
// 日历日期限制含当天的十四天后
const timeLimitation = () => {
  let date = new Date();
  date.setDate(date.getDate() + parseInt(state.mealListData.bookingDays || 13));
  state.maxDate = date;
};

const onChange = (val) => {
  if (val == 0) {
    router.push({ name: "home" });
  }
};
const orderEvent = (item) => {
  if (item.active == true) {
    const weekDay = state.selectDate.getDay();
    router.push({
      name: "orderingList",
      query: {
        reservationConfDate: moment(state.selectDate).format("YYYY-MM-DD"),
        reservationType: item.id,
        week: weekDay,
      },
    });
  }
};
</script>
<style lang="scss" scoped>
.ordering-dishes {
  width: 100%;
  .van-nav-bar {
    width: 100%;
  }
  .noActive {
    background: #c0c0c0;
  }
}
</style>
